import 'package:flutter/material.dart';
import 'package:hook_up_rent/widgets/common_image.dart';

class SearchBar extends StatefulWidget {
  final bool showLocation; //是否显示位置
  final Function goBackCallback; //回退
  final String inputValue; //搜索框的值
  final Function onCancal; //显示提示
  final bool showMap;
  final Function onSearch;
  final Function onSearchSubmit;

  const SearchBar(
      {Key key,
      this.showLocation,
      this.goBackCallback,
      this.inputValue,
      this.onCancal,
      this.showMap,
      this.onSearch,
      this.onSearchSubmit
      })
      : super(key: key);

  @override
  _SearchBarState createState() => _SearchBarState();
}

class _SearchBarState extends State<SearchBar> {

  String _searchWord = '';
  TextEditingController _searchController;
  FocusNode _focusNode;

  _onClean(){
    _searchController.clear();
    setState(() {
      _searchWord = '';
    });
  }

  @override
  void initState() {
    _searchController = TextEditingController(text:widget.inputValue);
    _focusNode = FocusNode();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Row(
      children: [
        if (widget.showLocation != null)
          GestureDetector(
            onTap: () {},
            child: Row(
              children: [
                Icon(
                  Icons.location_on,
                  color: Colors.green,
                ),
                Text(
                  "北京",
                  style: TextStyle(color: Colors.black, fontSize: 14),
                ),
                SizedBox(width: 10,)
              ],
            ),
          ),
        if (widget.goBackCallback != null)
          Padding(
            padding: const EdgeInsets.only(right: 10.0),
            child: GestureDetector(
              onTap: () {},
              child: Icon(
                Icons.keyboard_arrow_left,
                color: Colors.black,
              ),
            ),
          ),
        _buildSearchFelied(),
        if (widget.onCancal != null)
          Padding(
            padding: const EdgeInsets.only(left: 10),
            child: GestureDetector(
              onTap: () {},
              child: Text(
                "取消",
                style: TextStyle(color: Colors.black, fontSize: 14),
              ),
            ),
          ),
        if (widget.showMap != null)
          CommonImage('static/icons/widget_search_bar_map.png')
      ],
    );
  }

  //搜索框
  _buildSearchFelied() {
    return Expanded(
        child: Container(
            height: 34.0,
            decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(17.0),
                color: Colors.grey[200]),
            child: TextField(
              //使用焦点
              focusNode: _focusNode,
              onTap: (){
                if(widget.onSearchSubmit == null){
                  _focusNode.unfocus();
                }
                widget.onSearch();
              },
              onSubmitted: widget.onSearchSubmit,
              //激活键盘search
              textInputAction: TextInputAction.search,
              onChanged: (String value){
                setState(() {
                  _searchWord = value;
                });
              },
              controller: _searchController,
              style: TextStyle(fontSize: 14),
              decoration: InputDecoration(
                  contentPadding: EdgeInsets.only(right: -10.0),
                  prefixIcon: Icon(Icons.search, size: 20),
                  suffixIcon: GestureDetector(
                    onTap: _onClean,
                    child: Icon(Icons.clear, size: 20,color: _searchWord == '' ? Colors.grey[200] : Colors.grey),
                  ),
                  hintStyle: TextStyle(fontSize: 14),
                  border: InputBorder.none,
                  hintText: '请输入搜索词'),
            )));
  }
}

